#include "levelscene.h"
#include "gameconfig.h"
#include "levelscene.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include <QPainter>
#include <QLabel>
LevelScene::LevelScene(QWidget *parent) : QMainWindow(parent)
{
    this->setWindowTitle(tr("关卡选择"));
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowIcon(QIcon(GAME_ICON));
    //返回back按钮
    GamePushButton *btnBack = new GamePushButton(BTN_BACK_NORMAL,BTN_BACK_PRESSED);
    btnBack->setParent(this);
    btnBack->move(GAME_WIDTH - btnBack->width(),GAME_HEIGHT - btnBack->height());
//    btnBack->move(30,50);
    //关卡按钮
    for(int i = 0; i < 20;i++){
        GamePushButton* btnLevel = new GamePushButton(IMG_BG_LEVEL_ICON);
        btnLevel->setParent(this);
        btnLevel->move(65 + i%4 * 70,140 + i/4 * 70);
        int current_level = i + 1;
        QLabel* label = new QLabel(this);
        label->setParent(this);
        QFont f("宋体",15,Qt::black);
        label->setFont(f);
        label->setFixedSize(btnLevel->width(),btnLevel->height());
        label->setText(QString::number(current_level));
        label->move(65 + i%4 * 70,140 + i/4 * 70);
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

        //为每个关卡按钮设置点击事件
        connect(btnLevel,&GamePushButton::clicked,this,[=](){
            GameController::getInstance()->hideLevelScene();
            GameController::getInstance()->showGameScene(current_level);
            GameController::getInstance()->playStartSound();
        });
    }
    connect(btnBack,&GamePushButton::clicked,this,[=](){
       GameController::getInstance()->hideLevelScene();
       GameController::getInstance()->showMainScene();
       GameController::getInstance()->playBackSound();
    });
}



void LevelScene::paintEvent(QPaintEvent *event){
    QPainter levelPainter(this);
    QPixmap levelPixmap;
    bool result = levelPixmap.load(IMG_BG_LEVELSCENE);
    if(result){
        levelPainter.drawPixmap(0,0,levelPixmap);
    }
    //加载标题
    levelPixmap.load(IMG_GAME_TITLE);
    levelPainter.drawPixmap((this->width() - levelPixmap.width())/2,30,levelPixmap);
}
